package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSON;
import com.bdqn.tour.pojo.User;
import com.bdqn.tour.service.HzyInfoService;
import com.bdqn.tour.service.impl.HzyInfoServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/info")
public class InfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        // 获取 session 中的用户对象
        Object userObj = req.getSession().getAttribute("user");

        // 强转
        User user = (User) userObj;
        // 获取用户手机号
        String phone = user.getIpone();
        // 创建业务层对象
        HzyInfoService service = new HzyInfoServiceImpl();
        List<User> userInfo = service.getUserInfo(phone);
        //将数据转换为json
        String json = JSON.toJSONString(userInfo);
        resp.getWriter().write(json);
        resp.getWriter().close();

    }
}
